package es.kivitel.android.sms.gui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import es.kivitel.android.sms.R;
import es.kivitel.android.sms.kernel.DatabaseManager;

public class MainActivity extends Activity {
	public static final int CLOSING = 0;
	public static final String CLOSING_PARAM = "Close";	
	
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        InitActivity();
    }

    
    private void InitActivity(){
        try {
			DatabaseManager.initialize(this);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    	setContentView(R.layout.main);
    	
    	//Add an event button
	    final Button buttonCreateEvent = (Button) findViewById(R.id.btgoaddevent);
	    buttonCreateEvent.setOnClickListener(new View.OnClickListener() {
	    	// Perform action on click
	        public void onClick(View v) {
	        	Intent newEvent = new Intent(v.getContext(), NewEventActivity.class);
	        	startActivityForResult(newEvent, MainActivity.CLOSING);   
	        } 
	    });      
	    
	    // Watch events button
	    final Button buttonWatchEvent = (Button) findViewById(R.id.btgowatchevents);
	    buttonWatchEvent.setOnClickListener(new View.OnClickListener() {
	    	// Perform action on click
	        public void onClick(View v) {
	        	Intent month = new Intent(getApplicationContext(), MonthScheduler.class);
	        	startActivityForResult(month,MainActivity.CLOSING);           	
	        } 
	    }); 	    

	    // Go to configuration button
	    final Button buttonConfiguration = (Button) findViewById(R.id.btgoconfiguration);
	    buttonConfiguration.setOnClickListener(new View.OnClickListener() {
	    	// Perform action on click
	        public void onClick(View v) {	        	
	        	startActivityForResult(new Intent(v.getContext(),ConfigActivity.class),MainActivity.CLOSING);
	        } 
	    }); 	    
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	super.onActivityResult(requestCode, resultCode, data);
    	Bundle extras = getIntent().getExtras();
    	if(extras != null)
    		if(extras.getInt(MainActivity.CLOSING_PARAM) == 1)
    			finish();  	
    }
}